package com.tianbing.adlottery.controller.servantadmin;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.jubao.ejb.business.StoreBusiness;
import com.jubao.ejb.business.UserBusiness;
import com.jubao.ejb.dao.StoreDao;
import com.jubao.ejb.entity.Coupon;
import com.jubao.ejb.entity.Store;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UserSession;

/**
 * MultiActionController for admin my store.
 * 
 * @author Conan Chen
 * @since 2007.12.15
 */
public class AdminServantController extends MultiActionController {

	Logger logger = Logger.getLogger(AdminServantController.class.getName());

	private String serveStoreListView;

	private String serveStoreCouponListView;


	private UserBusiness userBusiness;

	private StoreBusiness storeBusiness;

	private StoreDao storeDao;

	private SkinUtils skinUtils;

	public ModelAndView listServeStore(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(serveStoreListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		// get site2content for this store
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);

		List<Store> storeList = userBusiness.getServeStoreList(auth.getUserSysId(),-1, pgno, pgsize);
		long totalCount = userBusiness.getServeStoreCount(auth.getUserSysId(),-1);
		model.put("storeList", storeList);

		int pageItemNum = storeList == null ? 0 : storeList.size();
		String otherParam = null;
		model = skinUtils
				.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pgsize, otherParam, model);

		return mv;
	}

	public ModelAndView listServeStoreCoupon(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(serveStoreCouponListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		// get site2content for this store
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);

		long stosid = ServletRequestUtils.getLongParameter(request, "stosid", -1);
		Store store = storeDao.find(stosid);

		List<Coupon> couponList = storeBusiness.getCouponList(stosid, pgno, pgsize);
		long totalCount = storeBusiness.getCouponCount(stosid);

		model.put("store", store);
		model.put("couponList", couponList);

		int pageItemNum = couponList == null ? 0 : couponList.size();
		String otherParam = null;
		model = skinUtils
				.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pgsize, otherParam, model);

		return mv;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setServeStoreListView(String serveStoreListView) {
		this.serveStoreListView = serveStoreListView;
	}

	public void setUserBusiness(UserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setServeStoreCouponListView(String serveStoreCouponListView) {
		this.serveStoreCouponListView = serveStoreCouponListView;
	}

	public void setStoreBusiness(StoreBusiness storeBusiness) {
		this.storeBusiness = storeBusiness;
	}

	public void setStoreDao(StoreDao storeDao) {
		this.storeDao = storeDao;
	}

}
